<?php
/**
 * User: Mykola B.
 * Date: 3/1/13
 * Time: 7:55 PM
 */
class DataBase
{
    public $link;
    private static $instance;

    private function __construct() {
        $settings = Settings::getSettings();
        $dbSettings = $settings->database;
        $this->link = new DB\SQL(
            "mysql:host=$dbSettings->host;port=$dbSettings->port;dbname=$dbSettings->dbname",
            $dbSettings->user,
            $dbSettings->password
        );
        return $this;
    }

    public static function getConnection() {
        if (empty(self::$instance)) {
            self::$instance = new DataBase();
        }
        return self::$instance;
    }

    /**
     * Redirect method calls to $f3 DB
     * @param $name
     * @param $args
     */
    public function __call($name, $args) {
        if (is_callable(array($this->link, $name), true)) {
            return call_user_func_array(array($this->link, $name), $args);
        }
    }
}
